package com.holly.common_view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.holly.common_view.R;
import com.holly.common_view.view.LimitLengthEditLayout;

/**
 * 2016/12/26 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class EditDialog extends Dialog {
    private TextView mTvTitle;
    private TextView mTvCancel;
    private TextView mTvOk;

    private OnEditDialogListener mOnEditDialogListener;
    private LimitLengthEditLayout mEditLayout;

    public EditDialog(Context context) {
        super(context, R.style.MAlertDialog);
        init();
    }

    private void init() {
        //点击屏幕外关闭
        setCanceledOnTouchOutside(true);
        Context themeContext = getContext();
        //设置为底部
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);

        View view = LayoutInflater.from(themeContext).inflate(R.layout.dialog_edit, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(view, layoutParams);
        initView(view);
    }

    private void initView(View view) {
        mTvTitle = (TextView) view.findViewById(R.id.tv_title);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvOk = (TextView) view.findViewById(R.id.tv_ok);
        mEditLayout = (LimitLengthEditLayout)view.findViewById(R.id.limit_length_edit_layout);

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnEditDialogListener != null) {
                    mOnEditDialogListener.onCancel(EditDialog.this);
                }
            }
        });

        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnEditDialogListener != null) {
                    mOnEditDialogListener.onConfirm(EditDialog.this,mEditLayout.getText());
                }
            }
        });
    }

    public LimitLengthEditLayout getEditLayout() {
        return mEditLayout;
    }

    public void setMessage(String text){
        if (!TextUtils.isEmpty(text)) {
            mEditLayout.setText(text);
        } else {
            mEditLayout.setText("");
        }
    }

    public void setOnEditDialogListener(OnEditDialogListener onEditDialogListener) {
        mOnEditDialogListener = onEditDialogListener;
    }

    public interface OnEditDialogListener {
        void onCancel(DialogInterface dialog);

        void onConfirm(DialogInterface dialog, String editInfo);
    }
}
